// 微信app版本比较
function versionCompare(ver1, ver2) {
	var version1pre = parseFloat(ver1);
	var version2pre = parseFloat(ver2);
	var version1next = parseInt(ver1.replace(version1pre + ".", ""));
	var version2next = parseInt(ver2.replace(version2pre + ".", ""));
	if (version1pre > version2pre) return true;
	else if (version1pre < version2pre) return false;
	else {
		if (version1next > version2next) return true;
		else return false;
	}
}

function hexStringToArrayBuffer(r) {
	if (!r) return new ArrayBuffer(0);
	console.log(r);
	for (var e = new ArrayBuffer(r.length), t = new DataView(e), n = 0, o = 0, a = r.length; o < a; o += 2) {
		var i = parseInt(r.substr(o, 2), 16);
		t.setUint8(n, i), n++ 
	}
	
	return e
}
function isNullOrEmpty(value) {
	//是否为空
	return (value === null || value === '' || value === undefined) ? true : false;
}

function showToastHint(text) {
	uni.showToast({
		title: text,
		icon: 'none'
	});
} // ArrayBuffer转16进制字符串示例


function ab2hex(buffer) {
	var hexArr = Array.prototype.map.call(new Uint8Array(buffer), function(bit) {
		return ('00' + bit.toString(16)).slice(-2);
	});
	return hexArr.join('');
}

function inArray(arr, key, val) {
	for (let i = 0; i < arr.length; i++) {
		if (arr[i][key] === val) {
			return i;
		}
	}

	return -1;
}

function intiFindIndex() {
	if (!Array.prototype.findIndex) {
		Array.prototype.findIndex = function(predicate) {
			'use strict';

			if (this == null) {
				throw new TypeError('Array.prototype.findIndex called on null or undefined');
			}

			if (typeof predicate !== 'function') {
				throw new TypeError('predicate must be a function');
			}

			var list = Object(this);
			var length = list.length >>> 0;
			var thisArg = arguments[1];
			var value;

			for (var i = 0; i < length; i++) {
				value = list[i];

				if (predicate.call(thisArg, value, i, list)) {
					return i;
				}
			}

			return -1;
		};
	}
}

module.exports = {
	ab2hex: ab2hex,
	versionCompare: versionCompare,
	inArray: inArray,
	intiFindIndex: intiFindIndex,
	showToastHint: showToastHint,
	isNullOrEmpty:isNullOrEmpty,
	hexStringToArrayBuffer:hexStringToArrayBuffer
};
